<?php

class RatesController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('layoutadmin');
    }

    public function indexAction()
    {
        $all = new Application_Model_Rates();
        $rates = $all->getAllRates();
        $this->view->rates = $rates;    
    }

    public function addAction()
    {
        $form = new Application_Form_UpdateRates();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();
            if($form->isValid($postData)) {
                $data = array(
                              'name'   => $form->getValue('name'),
                              'amount' => $form->getValue('amount')
                );
                $add = new Application_Model_Rates();
                $add->addRate($data);
                $this->_helper->redirector('index', 'rates');
            }
        }
    }    

    public function updateAction()
    {
        $id = $this->_getParam('id', 0);
        $form = new Application_Form_UpdateRates();
        $this->view->form = $form;
        $update = new Application_Model_Rates();
        $form->populate($update->getSelectRate($id));
        if ($this->getRequest()->isPost()) {
           $postData = $this->getRequest()->getPost();
           if ($form->isValid($postData)) {
               $data = array(
                             'name'   => $form->getValue('name'),
                             'amount' => $form->getValue('amount')
               );
               $update->updateRate($id, $data);
               $this->_helper->redirector('index', 'rates');
           }
        }   
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id');
        echo $id;
        $del = new Application_Model_Rates();
        $del->deleteRate($id);
        $this->_helper->redirector('index', 'rates');
    }
}